package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import javax.naming.NamingException;

import com.domain.Product;

public class ProductDAO extends BaseDAO {
	public int insert(Product product) {
		int result = 0;
		try {
			Connection conn = getConnection();
			String sql = "insert into product values(null, ?, ?, ?, ?)";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, product.getProductName());
			stmt.setFloat(2, product.getSalesPrice());
			stmt.setFloat(3, product.getBuyPrice());
			stmt.setString(4, product.getDescription());
			result = stmt.executeUpdate();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List<Product> getAllProduct() {
		List<Product> list = new LinkedList<Product>();
		try {
			Connection conn = getConnection();
			String sql = "select * from product";
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				int productCode = rs.getInt("productCode");
				String productName = rs.getString("productName");
				float salesPrice = rs.getFloat("salesPrice");
				float buyPrice = rs.getFloat("buyPrice");
				String description = rs.getString("description");
				list.add(new Product(productCode, productName, salesPrice,
						buyPrice, description));
			}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public Product getProductById(int code) {
		Product p = null;
		try {
			Connection conn = getConnection();
			String sql = "select * from product where productcode=?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setInt(1, code);
			ResultSet rs = stmt.executeQuery();
			rs.next();
			if (!rs.wasNull()) {
				int productCode = rs.getInt("productCode");
				String productName = rs.getString("productName");
				float salesPrice = rs.getFloat("salesPrice");
				float buyPrice = rs.getFloat("buyPrice");
				String description = rs.getString("description");
				p = new Product(productCode, productName, salesPrice, buyPrice,
						description);
			}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}

	public void deleteProduct(Product product) {
		Product p = null;
		try {
			Connection conn = getConnection();
			String sql = "delete from product where productcode=? and productname=? and salesprice=? and buyprice=? and description=? ";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setInt(1, product.getProductCode());
			stmt.setString(2, product.getProductName());
			stmt.setFloat(3, product.getSalesPrice());
			stmt.setFloat(4, product.getBuyPrice());
			stmt.setString(5, product.getDescription());
			stmt.executeUpdate();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
